<template>
  <el-dialog
    :title="!houses.jid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="houses" :rules="dataRule" ref="houses" @keyup.enter.native="housesSubmit()" label-width="80px">
    <el-form-item label="评论人" prop="phone">
      <el-input v-model="houses.phone" placeholder="评论人"></el-input>
    </el-form-item>
    <el-form-item label="经纪人" prop="username">
      <el-input v-model="houses.username" placeholder="经纪人"></el-input>
    </el-form-item>
    <el-form-item label="评论内容" prop="content">
      <el-input v-model="houses.content" placeholder="评论内容"></el-input>
    </el-form-item>
    <el-form-item label="评论时间" prop="startTime">
      <el-input v-model="houses.startTime" placeholder="评论时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="houses.status" placeholder="状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="housesSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        houses: {
          jid: 0,
          phone: '',
          username: '',
          content: '',
          startTime: '',
          status: ''
        },
        dataRule: {
          phone: [
            { required: true, message: '评论人不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '经纪人不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '评论内容不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '评论时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.houses.jid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['houses'].resetFields()
          if (this.houses.jid) {
            this.$http({
              url: this.$http.adornUrl(`/personage/remark/info/${this.houses.jid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.houses.phone = data.remark.phone
                this.houses.username = data.remark.username
                this.houses.content = data.remark.content
                this.houses.startTime = data.remark.startTime
                this.houses.status = data.remark.status
              }
            })
          }
        })
      },
      // 表单提交
      housesSubmit () {
        this.$refs['houses'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/personage/remark/${!this.houses.jid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'jid': this.houses.jid || undefined,
                'phone': this.houses.phone,
                'username': this.houses.username,
                'content': this.houses.content,
                'startTime': this.houses.startTime,
                'status': this.houses.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
